package com.rdzn.mall.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.rdzn.mall.center.entity.SmsCouponHistory;
import com.rdzn.mall.center.entity.SmsCouponProductCategoryRelation;
import com.rdzn.mall.center.entity.SmsCouponProductRelation;
import com.rdzn.mall.center.service.ISmsCouponHistoryService;
import com.rdzn.mall.center.service.ISmsCouponProductCategoryRelationService;
import com.rdzn.mall.center.service.ISmsCouponProductRelationService;
import com.rdzn.mall.center.service.ISmsCouponService;
import com.rdzn.mall.portal.domain.SmsCouponHistoryDetail;
import com.rdzn.mall.portal.service.ICouponHistoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CouponHistoryServiceImpl implements ICouponHistoryService {

    @Autowired
    private ISmsCouponHistoryService couponHistoryService;
    @Autowired
    private ISmsCouponService couponService;
    @Autowired
    private ISmsCouponProductRelationService couponProductRelationService;
    @Autowired
    private ISmsCouponProductCategoryRelationService couponProductCategoryRelationService;

    @Override
    public List<SmsCouponHistoryDetail> getDetailList(Integer memberId) {
        List<SmsCouponHistoryDetail> couponHistoryDetailList = new ArrayList<>();
        List<SmsCouponHistory> couponHistoryList = couponHistoryService.list(new LambdaQueryWrapper<SmsCouponHistory>().eq(SmsCouponHistory::getMemberId, memberId).eq(SmsCouponHistory::getUseStatus, 0));
        if (CollectionUtils.isNotEmpty(couponHistoryList)){
            couponHistoryList.forEach(smsCouponHistory -> {
                SmsCouponHistoryDetail couponHistoryDetail = new SmsCouponHistoryDetail();
                BeanUtils.copyProperties(smsCouponHistory, couponHistoryDetail);
                couponHistoryDetail.setCoupon(couponService.getById(smsCouponHistory.getCouponId()));
                couponHistoryDetail.setProductRelationList(couponProductRelationService.list(new LambdaQueryWrapper<SmsCouponProductRelation>().eq(SmsCouponProductRelation::getCouponId, smsCouponHistory.getCouponId())));
                couponHistoryDetail.setCategoryRelationList(couponProductCategoryRelationService.list(new LambdaQueryWrapper<SmsCouponProductCategoryRelation>().eq(SmsCouponProductCategoryRelation::getCouponId, smsCouponHistory.getCouponId())));
                couponHistoryDetailList.add(couponHistoryDetail);
            });
        }
        return couponHistoryDetailList;
    }

}
